<?php


namespace app\index\model;


use think\Model;

class Role extends Model
{
    /**
     * getRoleCascadeList
     * 获取权限列表
     * @url
     * @method
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author Asira
     * @date 2021/1/25
     */
    public static function getRoleCascadeList(): array
    {
        $RoleClass=new self();
        $data=$RoleClass->alias('r')
            ->join('role_detail rd','r.id=rd.role_id and rd.del_flag=0','left')
            ->where('r.del_flag',0)
            ->field('r.id,r.role_name,rd.id role_detail_id,rd.role_name role_detail_name')
            ->select()->toArray();
        $list=[];
        foreach ($data as $key=>$value){
            if(!isset($list[$value['id']])) {
                $list[$value['id']] = [
                    'value' => $value['id'],
                    'label' => $value['role_name'],
                ];
            }
            if($value['role_detail_id']) {
                $list[$value['id']]['children'][] = [
                    'value' => $value['role_detail_id'],
                    'label' => $value['role_detail_name'],
                ];
            }
        }
        $list=array_values($list);
        return $list;
    }

}